package com.white.controller;

import com.white.bean.Article;
import com.white.bean.Classify;
import com.white.bean.User;
import com.white.service.ArticleService;
import com.white.service.ClassifyService;
import com.white.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommonController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ClassifyService classifyService;
    @Autowired
    UserService userService;

    @GetMapping("/fails.html")
    public String redirectFails() {
        return "fails";
    }

    @GetMapping("/home.html")
    public String redirectHome(HttpSession session) {
        if (session.getAttribute("user") != null) {
            return "home";
        }
        return "fails";
    }

    @GetMapping("/home")
    public String home() {
        return "home";
    }


    @GetMapping("/head")
    public String head() {
        return "head";
    }

    @GetMapping("/dynamic")
    public String dynamic() {
        return "dynamic";
    }

    @GetMapping("/search")
    public String search() {
        return "search";
    }

    @GetMapping("/search/{content}/{type}")
    @ResponseBody
    public Object search(@PathVariable("content") String content,
                       @PathVariable("type") String type) {
        Object res = null;
        type = type.trim();
        /*模糊查询*/
        content = "%" + content + "%";
        /*返回文章数据*/
        switch (type) {
            case "article":
                List<Article> articles = articleService.search(content);
                if (articles.size() == 0) {
                    articles = articleService.getHotArticle();
                }
                res = articles;
                break;
            case "classify":
                List<Classify> classifies = classifyService.search(content);
                if (classifies.size() == 0) {
                    classifies = classifyService.list();
                }
                res = classifies;
                break;
            case "user":
                List<User> users = userService.mySearch(content);
                if (users.size() == 0) {
                    users = userService.list();
                }
                res = users;
                break;
        }
        return res;
    }

    @GetMapping("/component")
    public String component() {
        return "component";
    }

    @GetMapping("/app")
    public String app() {
        return "app";
    }

    @GetMapping("/vue")
    public String vue() {
        return "vue3";
    }

    @GetMapping("/myClassify")
    public String myClassify() {
        return "myClassify";
    }


    @GetMapping("/myFollow")
    public String myFollow() {
        return "myFollow";
    }

    @GetMapping("/myMessage")
    public String myMessage() {
        return "myMessage";
    }
}
